package org.yyf.resource.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.yyf.resource.demo.mybatis.entity.User;
import org.yyf.resource.demo.mybatis.mapper.UserMapper;
import org.yyf.resource.util.IdUtil;

/**
 * 测试controller
 * 用户测试接口方式测试mybatis 和 mybatisPlus
 * 省略了service层 直接调用mapper
 * @author yyf
 * @date 2019年8月29日
 */
@RestController
@RequestMapping("test")
public class MainController {
	
	@Autowired
	UserMapper userMapper;
	
	@RequestMapping(value = "{id}" , method = RequestMethod.GET)
	public User testQuery(@PathVariable String id) {
		return userMapper.get(id);
	}

	@RequestMapping(method = RequestMethod.PUT)
	public int testUpdate(@RequestBody User user) {
		try {
			userMapper.update(user);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public int testInsert(@RequestBody User user) {
		try {
			user.setId(IdUtil.createUUID());
			userMapper.insert(user);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
	
	@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
	public int testDelete(@PathVariable String id) {
		try {
			userMapper.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
	
}
